package com.ycsj.goldmedalnewconcept.student.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.common.util.StateCodeUtils;
import com.ycsj.goldmedalnewconcept.student.adapter.ScoreAdapter;
import com.ycsj.goldmedalnewconcept.student.bean.ScoreInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的成绩
 * Created by Administrator on 2019/5/8 0008.
 */

public class StuentMyScoreActivity extends BaseActivity implements SrfManager.OnPullRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srf)
    SmartRefreshLayout srf;
    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //数据源
    private List<ScoreInfo.ScoreListBean> oldDatas;

    //空视图
    private View emptyView;

    //数据适配器
    private ScoreAdapter adapter;


    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_myscore;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化标题
        initTitle();

        //初始化下拉刷新
        initSrf();

        //初始化列表
        initRv();

        //请求数据
        http(BqaManager.NORML);

    }


    /**
     * 初始化标题
     */
    private void initTitle() {
        tvTitle.setText("我的成绩");
    }

    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 初始化列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        adapter = new ScoreAdapter(R.layout.rv_item_st_score, oldDatas);
    }

    /**
     * 请求数据
     *
     * @param method
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "7");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "2");

        HttpUtils.post(HttpAddress.MYSCORE, map, this, new HttpCallBack<ScoreInfo>() {
            @Override
            public void onSuccess(ScoreInfo scoreInfo) {
                if (rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if ("200".equals(scoreInfo.state) || "104".equals(scoreInfo.state)) {
                    List<ScoreInfo.ScoreListBean> newDatas = scoreInfo.score_list;
                    if (method == BqaManager.NORML) {
                        if ("200".equals(scoreInfo.state)) {
                            //返回有数据
                            oldDatas.addAll(newDatas);
                        }
                        BqaManager.setRv(emptyView, StuentMyScoreActivity.this, adapter, rv);
                    } else {
                        BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                    }
                } else {
                    StateCodeUtils.showSateCode(scoreInfo.state);
                }
            }

            @Override
            public void onError(String e) {
                if (rlLoading.getVisibility() != View.GONE) {
                    rlLoading.setVisibility(View.GONE);
                }
                ToastUtils.showShort(BaseApplication.ERROR);
            }
        });
    }

    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.lv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
        }
    }
}
